package com.woniuxy.service.imlp;

import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.ManagerDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.service.ManagerService;
import com.woniuxy.utils.JWTUtil;
import com.woniuxy.utils.Resout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public  class ManagerServiceImlp implements ManagerService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired(required=false)
    private ManagerDao managerDao;

    @Override
    public Resout add(RbacManager manager) {
        Resout listResponseEntity = new Resout();
        if (managerDao.add(manager)) {
            listResponseEntity.setCode(200);
            listResponseEntity.setMsg("添加成功！");
        } else {
            listResponseEntity.setData(null);
            listResponseEntity.setCode(500);
            listResponseEntity.setMsg("添加失败");
        }
        return listResponseEntity;
    }

    @Override
    public Resout deleteById(int id) {
        Resout listResponseEntity = new Resout();
        if (managerDao.deleteById(id)) {
            listResponseEntity.setCode(200);
            listResponseEntity.setMsg("删除成功！");
        } else {
            listResponseEntity.setData(null);
            listResponseEntity.setCode(500);
            listResponseEntity.setMsg("删除失败");
        }
        return listResponseEntity;
    }

    @Override
    public Resout findAll(int currentPage, int pageSize, RbacManager manager) {
        PageHelper.startPage(currentPage, pageSize);
        List<RbacManager> rbacManagerList = managerDao.findAll(manager);
        PageInfo<RbacManager> of = PageInfo.of(rbacManagerList);
        Resout listResponseEntity = new Resout();
        if (rbacManagerList.size() > 0) {
            listResponseEntity.setData(of);
            listResponseEntity.setCode(200);
            listResponseEntity.setMsg("查询成功！");
        } else {
            listResponseEntity.setData(null);
            listResponseEntity.setCode(500);
            listResponseEntity.setMsg("数据为空");
        }
        return listResponseEntity;
    }
    @Override
    public Resout update(RbacManager rbacManager) {
         Resout listResponseEntity = new Resout();
        if (managerDao.update(rbacManager)) {
            listResponseEntity.setCode(200);
            listResponseEntity.setMsg("查询成功！");
        } else {
            listResponseEntity.setCode(500);
            listResponseEntity.setMsg("数据为空");
        }
        return listResponseEntity;
    }

    @Override
    public String findPassword(String username) {
        String password = managerDao.findPassword(username);
        return null;
    }

    /**
     * 查询指定用户
     *
     * @param rbacManager
     * @return
     */
    @Override
    public Resout login(RbacManager rbacManager) {
        RbacManager r = managerDao.login(rbacManager);
        if (r.getId()!=null) {
            String token = JWTUtil.sign(rbacManager.getAccount(), rbacManager.getPassword());
            r.setPassword("");
            r.setToken(token);
            //通过token存入对应的所属对象
            redisTemplate.opsForValue().set(token,r);
            //设置token过期时间 模拟使用那0s
            redisTemplate.expire(token,30, TimeUnit.SECONDS);
            return new Resout("登录成功",200,r);
        }else{
            return new Resout("账号或密码错误",404,null);
        }
    }
}
